package com.sxjh.service.financialrepost.impl;

import cn.hutool.extra.cglib.CglibUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxjh.entity.bo.FinanceHgylCgBo;
import com.sxjh.entity.param.BalanceRepostParam;
import com.sxjh.entity.result.FinanceHgylCgResult;
import com.sxjh.entity.param.FinancialSalesRepostParam;
import com.sxjh.entity.result.*;
import com.sxjh.mapper.FinancialRepostMapper;
import com.sxjh.service.financialrepost.FinancialRepostService;
import com.sxjh.uitl.PageUtil;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zxz
 * @Date 2021/11/19
 * @Description:
 */
@Service
public class FinancialRepostServiceImpl implements FinancialRepostService {
    private Logger logger = LoggerFactory.getLogger(FinancialRepostServiceImpl.class);
    @Autowired
    private FinancialRepostMapper financialRepostMapper;

    /**
     * 财务销售对账产品
     *
     * @param param
     * @return
     */
    @Override
    public List<FinancialRepostResult> getReconciliationCustomerRepost(FinancialSalesRepostParam param) {
        List<FinancialRepostResult> repostResults = new ArrayList<>();
        List<FinancialRepostResult> repostResultList;
        if (param.getMaterialId() != null && param.getMaterialId() == 18) {
            repostResultList = financialRepostMapper.getFinancialZcSalesRepost(param);
        } else {
            repostResultList = financialRepostMapper.getFinancialSalesRepost(param);
        }
        if (repostResultList.size() < 1) {
            return repostResultList;
        }
        // 以物料做分组
        Map<String, List<FinancialRepostResult>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(FinancialRepostResult::getMaterialName, Collectors.toList()));
        FinancialRepostResult financialRepostHJ = new FinancialRepostResult();
        financialRepostHJ.setMaterialName("合计");
        for (String str : collect1.keySet()) {
            // 再以物料做分组下再以公司做分组
            Map<String, List<FinancialRepostResult>> collect2 = collect1.get(str).stream()
                    .collect(Collectors.groupingBy(FinancialRepostResult::getCompanyName, Collectors.toList()));
            FinancialRepostResult financialRepostXJ = new FinancialRepostResult();
            financialRepostXJ.setCompanyName("小计");
            float sumXJ = 0;
            float sumHJ = 0;
            for (String str1 : collect2.keySet()) {
                // 公司累计
                FinancialRepostResult financialRepostGS = new FinancialRepostResult();
                for (FinancialRepostResult financialRepostResult : collect2.get(str1)) {
                    // 赋值物料名称
                    financialRepostGS.setMaterialName(financialRepostResult.getMaterialName());
                    // 赋值公司名称
                    financialRepostGS.setCompanyName(financialRepostResult.getCompanyName());
                    // 累计公司的货重
                    financialRepostGS.setWeighbridgeWeight(financialRepostGS.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));
                    // 赋值公司单价
//                    financialRepostGS.setSettlementPrice(financialRepostResult.getSettlementPrice());
                    // 累计公司金额
                    financialRepostGS.setMoney(financialRepostGS.getMoney().add(financialRepostResult.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                    // 累计公司垫付运费
                    financialRepostGS.setAdvance(financialRepostGS.getAdvance().add(financialRepostResult.getAdvance()));
                    // 累计公司装车费
                    financialRepostGS.setAssembleMoney(financialRepostGS.getAssembleMoney().add(financialRepostResult.getAssembleMoney()));
                    // 累计公司包装费
                    financialRepostGS.setPack(financialRepostGS.getPack().add(financialRepostResult.getPack()));
                    // 累计公司其他费用
                    financialRepostGS.setOther(financialRepostGS.getOther().add(financialRepostResult.getOther()));
                    // 累计公司合计费用
                    financialRepostGS.setMoneySum(financialRepostGS.getMoneySum().add(financialRepostResult.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));

                }
                // 赋值公司单价
                financialRepostGS.setSettlementPrice(financialRepostGS.getMoney().divide(financialRepostGS.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
                repostResults.add(financialRepostGS);
                // 赋值物料名称
                financialRepostXJ.setMaterialName(financialRepostGS.getMaterialName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostGS.getWeighbridgeWeight()));
                // 累计公司金额
                financialRepostXJ.setMoney(financialRepostXJ.getMoney().add(financialRepostGS.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                // 累计公司垫付运费
                financialRepostXJ.setAdvance(financialRepostXJ.getAdvance().add(financialRepostGS.getAdvance()));
                // 累计公司装车费
                financialRepostXJ.setAssembleMoney(financialRepostXJ.getAssembleMoney().add(financialRepostGS.getAssembleMoney()));
                // 累计公司包装费
                financialRepostXJ.setPack(financialRepostXJ.getPack().add(financialRepostGS.getPack()));
                // 累计公司其他费用
                financialRepostXJ.setOther(financialRepostXJ.getOther().add(financialRepostGS.getOther()));
                // 累计公司合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostGS.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));
            }
            // 赋值公司单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getMoney().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));
            // 累计公司金额
            financialRepostHJ.setMoney(financialRepostHJ.getMoney().add(financialRepostXJ.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
            // 累计公司垫付运费
            financialRepostHJ.setAdvance(financialRepostHJ.getAdvance().add(financialRepostXJ.getAdvance()));
            // 累计公司装车费
            financialRepostHJ.setAssembleMoney(financialRepostHJ.getAssembleMoney().add(financialRepostXJ.getAssembleMoney()));
            // 累计公司包装费
            financialRepostHJ.setPack(financialRepostHJ.getPack().add(financialRepostXJ.getPack()));
            // 累计公司其他费用
            financialRepostHJ.setOther(financialRepostHJ.getOther().add(financialRepostXJ.getOther()));
            // 累计公司合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getMoney().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    /**
     * 财务销售对账客户
     *
     * @param param
     * @return
     */
    @Override
    public List<FinancialRepostResult> getReconciliationProductRepost(FinancialSalesRepostParam param) {
        List<FinancialRepostResult> repostResults = new ArrayList<>();
        List<FinancialRepostResult> repostResultList;
        if (param.getMaterialId() != null && param.getMaterialId() == 18) {
            repostResultList = financialRepostMapper.getFinancialZcSalesRepost(param);
        } else {
            repostResultList = financialRepostMapper.getFinancialSalesRepost(param);
        }
        if (repostResultList.size() < 1) {
            return repostResultList;
        }
        // 以公司做分组
        Map<String, List<FinancialRepostResult>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(FinancialRepostResult::getCompanyName, Collectors.toList()));
        FinancialRepostResult financialRepostHJ = new FinancialRepostResult();
        financialRepostHJ.setCompanyName("合计");
        for (String str : collect1.keySet()) {
            // 再以公司做分组下再以物料做分组
            Map<String, List<FinancialRepostResult>> collect2 = collect1.get(str).stream()
                    .collect(Collectors.groupingBy(FinancialRepostResult::getMaterialName, Collectors.toList()));
            FinancialRepostResult financialRepostXJ = new FinancialRepostResult();
            financialRepostXJ.setMaterialName("小计");
            for (String str1 : collect2.keySet()) {
                // 公司累计
                FinancialRepostResult financialRepostGS = new FinancialRepostResult();
                for (FinancialRepostResult financialRepostResult : collect2.get(str1)) {
                    // 赋值物料名称
                    financialRepostGS.setMaterialName(financialRepostResult.getMaterialName());
                    // 赋值公司名称
                    financialRepostGS.setCompanyName(financialRepostResult.getCompanyName());
                    // 累计公司的货重
                    financialRepostGS.setWeighbridgeWeight(financialRepostGS.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));
                    // 累计公司金额
                    financialRepostGS.setMoney(financialRepostGS.getMoney().add(financialRepostResult.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                    // 累计公司垫付运费
                    financialRepostGS.setAdvance(financialRepostGS.getAdvance().add(financialRepostResult.getAdvance()));
                    // 累计公司装车费
                    financialRepostGS.setAssembleMoney(financialRepostGS.getAssembleMoney().add(financialRepostResult.getAssembleMoney()));
                    // 累计公司包装费
                    financialRepostGS.setPack(financialRepostGS.getPack().add(financialRepostResult.getPack()));
                    // 累计公司其他费用
                    financialRepostGS.setOther(financialRepostGS.getOther().add(financialRepostResult.getOther()));
                    // 累计公司合计费用
                    financialRepostGS.setMoneySum(financialRepostGS.getMoneySum().add(financialRepostResult.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));

                }
                // 赋值公司单价
                financialRepostGS.setSettlementPrice(financialRepostGS.getMoney().divide(financialRepostGS.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
                repostResults.add(financialRepostGS);
                // 赋值公司名称
                financialRepostXJ.setCompanyName(financialRepostGS.getCompanyName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostGS.getWeighbridgeWeight()));
                // 累计公司金额
                financialRepostXJ.setMoney(financialRepostXJ.getMoney().add(financialRepostGS.getMoney()));
                // 累计公司垫付运费
                financialRepostXJ.setAdvance(financialRepostXJ.getAdvance().add(financialRepostGS.getAdvance()));
                // 累计公司装车费
                financialRepostXJ.setAssembleMoney(financialRepostXJ.getAssembleMoney().add(financialRepostGS.getAssembleMoney()));
                // 累计公司包装费
                financialRepostXJ.setPack(financialRepostXJ.getPack().add(financialRepostGS.getPack()));
                // 累计公司其他费用
                financialRepostXJ.setOther(financialRepostXJ.getOther().add(financialRepostGS.getOther()));
                // 累计公司合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostGS.getMoneySum()));
            }
            // 赋值公司单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getMoney().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));
            // 累计公司金额
            financialRepostHJ.setMoney(financialRepostHJ.getMoney().add(financialRepostXJ.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
            // 累计公司垫付运费
            financialRepostHJ.setAdvance(financialRepostHJ.getAdvance().add(financialRepostXJ.getAdvance()));
            // 累计公司装车费
            financialRepostHJ.setAssembleMoney(financialRepostHJ.getAssembleMoney().add(financialRepostXJ.getAssembleMoney()));
            // 累计公司包装费
            financialRepostHJ.setPack(financialRepostHJ.getPack().add(financialRepostXJ.getPack()));
            // 累计公司其他费用
            financialRepostHJ.setOther(financialRepostHJ.getOther().add(financialRepostXJ.getOther()));
            // 累计公司合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getMoney().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    /**
     * 财务销售对账开票
     *
     * @param param
     * @return
     */
    @Override
    public List<FinancialRepostResult> getReconciliationInvoicingRepost(FinancialSalesRepostParam param) {
        List<FinancialRepostResult> repostResults = new ArrayList<>();
        List<FinancialRepostResult> repostResultList;
        if (param.getMaterialId() != null && param.getMaterialId() == 18) {
            repostResultList = financialRepostMapper.getFinancialZcSalesRepost(param);
        } else {
            repostResultList = financialRepostMapper.getFinancialSalesRepost(param);
        }
        if (repostResultList.size() < 1) {
            return repostResultList;
        }
        // 以物料做分组
        Map<String, List<FinancialRepostResult>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(FinancialRepostResult::getCompanyName, Collectors.toList()));
        FinancialRepostResult financialRepostHJ = new FinancialRepostResult();
        financialRepostHJ.setCompanyName("合计");
        for (String str : collect1.keySet()) {
            // 再以物料做分组下再以公司做分组
            Map<String, List<FinancialRepostResult>> collect2 = collect1.get(str).stream()
                    .collect(Collectors.groupingBy(item -> item.getSettlementPrice() + "_" + item.getMaterialName()));
            FinancialRepostResult financialRepostXJ = new FinancialRepostResult();
            financialRepostXJ.setMaterialName("小计");
            for (String str1 : collect2.keySet()) {
                // 公司累计
                FinancialRepostResult financialRepostGS = new FinancialRepostResult();
                for (FinancialRepostResult financialRepostResult : collect2.get(str1)) {
                    // 赋值物料名称
                    financialRepostGS.setMaterialName(financialRepostResult.getMaterialName());
                    // 赋值公司名称
                    financialRepostGS.setCompanyName(financialRepostResult.getCompanyName());
                    // 累计公司的货重
                    financialRepostGS.setWeighbridgeWeight(financialRepostGS.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));
                    // 赋值公司单价
                    financialRepostGS.setSettlementPrice(financialRepostResult.getSettlementPrice());
                    // 累计公司金额
                    financialRepostGS.setMoney(financialRepostGS.getMoney().add(financialRepostResult.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                    // 累计公司垫付运费
                    financialRepostGS.setAdvance(financialRepostGS.getAdvance().add(financialRepostResult.getAdvance()));
                    // 累计公司装车费
                    financialRepostGS.setAssembleMoney(financialRepostGS.getAssembleMoney().add(financialRepostResult.getAssembleMoney()));
                    // 累计公司包装费
                    financialRepostGS.setPack(financialRepostGS.getPack().add(financialRepostResult.getPack()));
                    // 累计公司其他费用
                    financialRepostGS.setOther(financialRepostGS.getOther().add(financialRepostResult.getOther()));
                    // 累计公司合计费用
                    financialRepostGS.setMoneySum(financialRepostGS.getMoneySum().add(financialRepostResult.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));

                }
                repostResults.add(financialRepostGS);
                // 赋值公司名称
                financialRepostXJ.setCompanyName(financialRepostGS.getCompanyName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostGS.getWeighbridgeWeight()));
                // 累计公司金额
                financialRepostXJ.setMoney(financialRepostXJ.getMoney().add(financialRepostGS.getMoney()));
                // 累计公司垫付运费
                financialRepostXJ.setAdvance(financialRepostXJ.getAdvance().add(financialRepostGS.getAdvance()));
                // 累计公司装车费
                financialRepostXJ.setAssembleMoney(financialRepostXJ.getAssembleMoney().add(financialRepostGS.getAssembleMoney()));
                // 累计公司包装费
                financialRepostXJ.setPack(financialRepostXJ.getPack().add(financialRepostGS.getPack()));
                // 累计公司其他费用
                financialRepostXJ.setOther(financialRepostXJ.getOther().add(financialRepostGS.getOther()));
                // 累计公司合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostGS.getMoneySum()));
            }
            // 赋值公司单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getMoney().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));
            // 累计公司金额
            financialRepostHJ.setMoney(financialRepostHJ.getMoney().add(financialRepostXJ.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
            // 累计公司垫付运费
            financialRepostHJ.setAdvance(financialRepostHJ.getAdvance().add(financialRepostXJ.getAdvance()));
            // 累计公司装车费
            financialRepostHJ.setAssembleMoney(financialRepostHJ.getAssembleMoney().add(financialRepostXJ.getAssembleMoney()));
            // 累计公司包装费
            financialRepostHJ.setPack(financialRepostHJ.getPack().add(financialRepostXJ.getPack()));
            // 累计公司其他费用
            financialRepostHJ.setOther(financialRepostHJ.getOther().add(financialRepostXJ.getOther()));
            // 累计公司合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getMoney().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    /**
     * 财务销售对账明细
     *
     * @param param
     * @return
     */
    @Override
    public List<FinancialRepostResult> getReconciliationDetailRepost(FinancialSalesRepostParam param) {
        List<FinancialRepostResult> repostResults = new ArrayList<>();
        List<FinancialRepostResult> repostResultList;
        if (param.getMaterialId() != null && param.getMaterialId() == 18) {
            repostResultList = financialRepostMapper.getFinancialZcSalesRepost(param);
        } else {
            repostResultList = financialRepostMapper.getFinancialSalesRepost(param);
        }
        if (repostResultList.size() < 1) {
            return repostResultList;
        }
        // 以物料做分组
        Map<String, List<FinancialRepostResult>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(FinancialRepostResult::getCompanyName, Collectors.toList()));
        FinancialRepostResult financialRepostHJ = new FinancialRepostResult();
        financialRepostHJ.setCompanyName("合计");
        for (String str : collect1.keySet()) {
            // 再以物料做分组下再以公司做分组
            FinancialRepostResult financialRepostXJ = new FinancialRepostResult();
            financialRepostXJ.setMaterialName("小计");
            for (FinancialRepostResult financialRepostResult : collect1.get(str)) {
                repostResults.add(financialRepostResult);
                // 赋值公司名称
                financialRepostXJ.setCompanyName(financialRepostResult.getCompanyName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));
                // 累计公司金额
                financialRepostXJ.setMoney(financialRepostXJ.getMoney().add(financialRepostResult.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                // 累计公司垫付运费
                financialRepostXJ.setAdvance(financialRepostXJ.getAdvance().add(financialRepostResult.getAdvance()));
                // 累计公司装车费
                financialRepostXJ.setAssembleMoney(financialRepostXJ.getAssembleMoney().add(financialRepostResult.getAssembleMoney()));
                // 累计公司包装费
                financialRepostXJ.setPack(financialRepostXJ.getPack().add(financialRepostResult.getPack()));
                // 累计公司其他费用
                financialRepostXJ.setOther(financialRepostXJ.getOther().add(financialRepostResult.getOther()));
                // 累计公司合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostResult.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));

            }
            // 赋值公司单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getMoney().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));
            // 累计公司金额
            financialRepostHJ.setMoney(financialRepostHJ.getMoney().add(financialRepostXJ.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
            // 累计公司垫付运费
            financialRepostHJ.setAdvance(financialRepostHJ.getAdvance().add(financialRepostXJ.getAdvance()));
            // 累计公司装车费
            financialRepostHJ.setAssembleMoney(financialRepostHJ.getAssembleMoney().add(financialRepostXJ.getAssembleMoney()));
            // 累计公司包装费
            financialRepostHJ.setPack(financialRepostHJ.getPack().add(financialRepostXJ.getPack()));
            // 累计公司其他费用
            financialRepostHJ.setOther(financialRepostHJ.getOther().add(financialRepostXJ.getOther()));
            // 累计公司合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getMoney().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    /**
     * 焦油采购报表
     *
     * @param param
     * @return
     */
    @Override
    public PageUtil<FinanceHgylCgResult> getChemicalRawMaterialPurchaseReport(FinancialSalesRepostParam param) {
        // 处理数据
        PageUtil<FinanceHgylCgBo> pageUtil = new PageUtil<>();
        PageUtil<FinanceHgylCgResult> resultPage = new PageUtil<>();
        List<FinanceHgylCgBo> list = new ArrayList<>();
        if(param.getCurr() != 0 && param.getPageSize() != 0){
            pageUtil.setPageNum(param.getCurr());
            pageUtil.setPageSize(param.getPageSize());
            PageUtil<FinanceHgylCgBo> page = pageUtil.setMyList(financialRepostMapper.getChemicalRawMaterialPurchaseReport(param));
            resultPage.setPageCount(page.getMyList().size());
            list = page.getData();
        }else{
            list = financialRepostMapper.getChemicalRawMaterialPurchaseReport(param);
        }
        // 新数据集合
        List<FinanceHgylCgResult> results = new ArrayList<>();
        // 小计
        FinanceHgylCgResult subtotal = new FinanceHgylCgResult();
        subtotal.setPlateNumber("小计");
        // 合计
        FinanceHgylCgResult total = new FinanceHgylCgResult();
        total.setPlateNumber("合计");
        String contract = null;
        if (list.size() > 0) contract = list.get(0).getContractNo();
        // 计算数据剩余值
        for (FinanceHgylCgBo bo : list) {
            FinanceHgylCgResult item = CglibUtil.copy(bo, FinanceHgylCgResult.class);
            // 水分
            item.setWater(getWater(bo.getResult()));
            // 水分扣重=实收-（（大于水分标准的水分-水分标准）*实收）
            item.setBearing(bo.getMoistureDeduction());
            // 水份扣款=水分扣重*单价
            item.setWaterWithhold(bo.getMoistureDeduction().multiply(item.getSettlementPrice()));
            // 磅差吨数
            // 实收-原发==0
            if (item.getWeighbridgeWeight().compareTo(item.getNetWeight()) == 0) {
                item.setTunnage(new BigDecimal(0));
            } else if (item.getWeighbridgeWeight().compareTo(item.getNetWeight()) == 1) {
                // 实收-原发<0
//                item.setTunnage(bo.getPoundDifference());
                item.setTunnage(new BigDecimal(0));
            } else {
                item.setTunnage(item.getNetWeight().subtract(item.getWeighbridgeWeight()));
            }
            // 磅差扣款=((磅差-0.08)+水分扣重)*单价
            // 磅差-0.08
            BigDecimal b1 = item.getTunnage().subtract(new BigDecimal(0.08));
            //(磅差-0.08)+水分扣重
            BigDecimal b2 = b1.add(item.getBearing());
            BigDecimal bigDecimal = b2.multiply(item.getSettlementPrice());
            if (item.getTunnage().compareTo(new BigDecimal(0)) == 0 || item.getTunnage().compareTo(new BigDecimal(0)) == -1 || b1.compareTo(new BigDecimal(0)) == -1) {
                item.setWithhold(new BigDecimal(0));
            } else {
                item.setWithhold(bigDecimal);
            }
            // 运费扣款
            if (item.getWater().compareTo(new BigDecimal(6.0)) == 1 && item.getWater().compareTo(new BigDecimal(8.0)) < 1) {
                BigDecimal sjyf = bo.getFreight().multiply(bo.getOldnetWeight());
                item.setFreightWithhold(sjyf.multiply(new BigDecimal(0.15)));
            } else if (item.getWater().compareTo(new BigDecimal(8.0)) == 1 && item.getWater().compareTo(new BigDecimal(10.0)) < 1) {
                BigDecimal sjyf = bo.getFreight().multiply(bo.getOldnetWeight());
                item.setFreightWithhold(sjyf.multiply(new BigDecimal(0.25)));
            } else if (item.getWater().compareTo(new BigDecimal(10.0)) == 1) {
                item.setFreightWithhold(new BigDecimal(0));
            } else {
                item.setFreightWithhold(bo.getFreight().multiply(bo.getOldnetWeight()));
            }
//            item.setFreightWithhold(item.getWaterWithhold().add(bigDecimal));
            // 计算磅差扣款(磅差扣款=磅差吨数*运费单价)
//            if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
//                item.setWithhold(bigDecimal.multiply(item.getFreight()));
//            }
            // 其他扣款(其他扣款*实收吨数)
            item.setOtherWithhold(item.getOtherWithhold().multiply(item.getWeighbridgeWeight()));
            // 合计扣款(磅差扣款+运费扣款+其他扣款)
            BigDecimal totalButtons = new BigDecimal(0);
            // 累加磅差扣款
            if (item.getWithhold() != null && item.getWithhold().compareTo(new BigDecimal(0)) == 1) {
                totalButtons = totalButtons.add(item.getWithhold());
            }
            // 累加水分扣款
            if (item.getWaterWithhold() != null && item.getWaterWithhold().compareTo(new BigDecimal(0)) == 1) {
                totalButtons = totalButtons.add(item.getWaterWithhold());
            }
            // 累加运费扣款
            if (item.getFreightWithhold() != null && item.getFreightWithhold().compareTo(new BigDecimal(0)) == 1) {
                totalButtons = totalButtons.add(item.getFreightWithhold());
            }
            // 累加其他扣款
            if (item.getOtherWithhold() != null && item.getOtherWithhold().compareTo(new BigDecimal(0)) == 1) {
                totalButtons = totalButtons.add(item.getOtherWithhold());
            }
            item.setSumWithhold(totalButtons);
            // 运杂费=原发数*运费单价-合计扣款
            BigDecimal ranspotrtFees = (bo.getOldnetWeight().multiply(item.getFreight())).subtract(item.getSumWithhold());
            item.setIncidentals(ranspotrtFees);
            // 金额
            item.setMoney(item.getWeighbridgeWeight().multiply(item.getSettlementPrice()));
            // 总金额
            item.setMoneySum(item.getMoney().add(item.getIncidentals()));
            // 运费扣款
//            item.setFreightWithhold(item.getWaterWithhold().add(bigDecimal));
            // 将处理后的数据填入返回集合
            results.add(item);
            // 保存小计数据，计入合计，重置小计对象
            if (!contract.equals(item.getContractNo())) {
                // 添加小计
                results.add(CglibUtil.copy(subtotal, FinanceHgylCgResult.class));
                // 计算合计
                total.setNetWeight(total.getNetWeight().add(subtotal.getNetWeight()));
                total.setWeighbridgeWeight(total.getWeighbridgeWeight().add(subtotal.getWeighbridgeWeight()));
                total.setMoney(total.getMoney().add(subtotal.getMoney()));
                total.setWithhold(total.getWithhold().add(subtotal.getWithhold()));
                total.setSumWithhold(total.getSumWithhold().add(subtotal.getSumWithhold()));
                total.setIncidentals(total.getIncidentals().add(subtotal.getIncidentals()));
                total.setMoneySum(total.getMoneySum().add(subtotal.getMoneySum()));
                // 更改判断合同号
                contract = item.getContractNo();
                // 清空小计
                subtotal.setPlateNumber("小计");
                subtotal.setNetWeight(new BigDecimal(0));
                subtotal.setWeighbridgeWeight(new BigDecimal(0));
                subtotal.setMoney(new BigDecimal(0));
                subtotal.setWithhold(new BigDecimal(0));
                subtotal.setSumWithhold(new BigDecimal(0));
                subtotal.setIncidentals(new BigDecimal(0));
                subtotal.setMoneySum(new BigDecimal(0));
            }
            // 统计小计
            // 原发数量
            if (item.getNetWeight() != null && item.getNetWeight().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setNetWeight(subtotal.getNetWeight().add(item.getNetWeight()));
            }
            // 实收数量
            if (item.getWeighbridgeWeight() != null && item.getWeighbridgeWeight().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setWeighbridgeWeight(subtotal.getWeighbridgeWeight().add(item.getWeighbridgeWeight()));
            }

            // 金额
            if (item.getMoney() != null && item.getMoney().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setMoney(subtotal.getMoney().add(item.getMoney()));
            }

            // 扣款
            if (item.getWithhold() != null && item.getWithhold().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setWithhold(subtotal.getWithhold().add(item.getWithhold()));
            }
            // 合计扣款
            if (item.getSumWithhold() != null && item.getSumWithhold().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setSumWithhold(subtotal.getSumWithhold().add(item.getSumWithhold()));
            }
            // 运杂费
            if (item.getIncidentals() != null && item.getIncidentals().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setIncidentals(subtotal.getIncidentals().add(item.getIncidentals()));
            }
            // 总金额
            if (item.getMoneySum() != null && item.getMoneySum().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setMoneySum(subtotal.getMoneySum().add(item.getMoneySum()));
            }
        }
//


//        // 小计
//        FinanceHgylCgResult subtotal = new FinanceHgylCgResult();
//        subtotal.setPlateNumber("小计");
//        // 合计
//        FinanceHgylCgResult total = new FinanceHgylCgResult();
//        total.setPlateNumber("合计");
//        String contract = null;
//        if (list.size() > 0) contract = list.get(0).getContractNo();
//        // 计算数据剩余值
//        for (FinanceHgylCgBo bo : list) {
//            FinanceHgylCgResult item = CglibUtil.copy(bo, FinanceHgylCgResult.class);
//            // 水分
//            BigDecimal bigDecimal1 = getWater(bo.getResult());
//            item.setWater(bigDecimal1);
//            // 水分扣重
//            item.setBearing(bo.getMoistureDeduction());
//            // 水份扣款
//            item.setWaterWithhold(bo.getMoistureDeduction().multiply(item.getFreight()));
//            // 磅差吨数
//            item.setTunnage(bo.getPoundDifference());
//            // 磅差扣款
//            BigDecimal bigDecimal = bo.getPoundDifference();
//            // 运费扣款
//            item.setFreightWithhold(item.getWaterWithhold().add(bigDecimal));
//            // 计算磅差扣款(磅差扣款=磅差吨数*运费单价)
//            if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
//                item.setWithhold(bigDecimal.multiply(item.getFreight()));
//            }
//            // 其他扣款(其他扣款*实收吨数)
//            item.setOtherWithhold(item.getOtherWithhold().multiply(item.getWeighbridgeWeight()));
//            // 合计扣款(磅差扣款+运费扣款+其他扣款)
//            BigDecimal bigDecimal2 = new BigDecimal(0);
//            // 累加磅差扣款
//            if (item.getWithhold() != null && item.getWithhold().compareTo(new BigDecimal(0)) == 1) {
//                bigDecimal2 = bigDecimal2.add(item.getWithhold());
//            }
//            // 累加水分扣款
//            if (item.getWaterWithhold() != null && item.getWaterWithhold().compareTo(new BigDecimal(0)) == 1) {
//                bigDecimal2 = bigDecimal2.add(item.getWaterWithhold());
//            }
//            // 累加运费扣款
//            if (item.getFreightWithhold() != null && item.getFreightWithhold().compareTo(new BigDecimal(0)) == 1) {
//                bigDecimal2 = bigDecimal2.add(item.getFreightWithhold());
//            }
//            // 累加其他扣款
//            if (item.getOtherWithhold() != null && item.getOtherWithhold().compareTo(new BigDecimal(0)) == 1) {
//                bigDecimal2 = bigDecimal2.add(item.getOtherWithhold());
//            }
//            item.setSumWithhold(bigDecimal2);
//            // 运杂费
//            BigDecimal bigDecimal3 = item.getWeighbridgeWeight().multiply(item.getFreight()).subtract(item.getSumWithhold());
//            item.setIncidentals(bigDecimal3);
//            // 运费扣款
//            item.setFreightWithhold(item.getWaterWithhold().add(bigDecimal));
//            // 将处理后的数据填入返回集合
//            results.add(item);
//            // 保存小计数据，计入合计，重置小计对象
//            if (!contract.equals(item.getContractNo())) {
//                // 添加小计
//                results.add(CglibUtil.copy(subtotal, FinanceHgylCgResult.class));
//                // 计算合计
//                total.setNetWeight(total.getNetWeight().add(subtotal.getNetWeight()));
//                total.setWeighbridgeWeight(total.getWeighbridgeWeight().add(subtotal.getWeighbridgeWeight()));
//                total.setMoney(total.getMoney().add(subtotal.getMoney()));
//                total.setWithhold(total.getWithhold().add(subtotal.getWithhold()));
//                total.setSumWithhold(total.getSumWithhold().add(subtotal.getSumWithhold()));
//                total.setIncidentals(total.getIncidentals().add(subtotal.getIncidentals()));
//                total.setMoneySum(total.getMoneySum().add(subtotal.getMoneySum()));
//                // 更改判断合同号
//                contract = item.getContractNo();
//                // 清空小计
//                subtotal.setPlateNumber("小计");
//                subtotal.setNetWeight(new BigDecimal(0));
//                subtotal.setWeighbridgeWeight(new BigDecimal(0));
//                subtotal.setMoney(new BigDecimal(0));
//                subtotal.setWithhold(new BigDecimal(0));
//                subtotal.setSumWithhold(new BigDecimal(0));
//                subtotal.setIncidentals(new BigDecimal(0));
//                subtotal.setMoneySum(new BigDecimal(0));
//            }
//            // 统计小计
//            // 原发数量
//            if (item.getNetWeight() != null && item.getNetWeight().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setNetWeight(subtotal.getNetWeight().add(item.getNetWeight()));
//            }
//            // 实收数量
//            if (item.getWeighbridgeWeight() != null && item.getWeighbridgeWeight().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setWeighbridgeWeight(subtotal.getWeighbridgeWeight().add(item.getWeighbridgeWeight()));
//            }
//            item.setMoney(item.getWeighbridgeWeight().multiply(item.getSettlementPrice()));
//            // 金额
//            if (item.getMoney() != null && item.getMoney().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setMoney(subtotal.getMoney().add(item.getMoney()));
//            }
//            // 总金额
//            item.setMoneySum(item.getMoney().add(item.getIncidentals()));
//            // 扣款
//            if (item.getWithhold() != null && item.getWithhold().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setWithhold(subtotal.getWithhold().add(item.getWithhold()));
//            }
//            // 合计扣款
//            if (item.getSumWithhold() != null && item.getSumWithhold().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setSumWithhold(subtotal.getSumWithhold().add(item.getSumWithhold()));
//            }
//            // 运杂费
//            if (item.getIncidentals() != null && item.getIncidentals().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setIncidentals(subtotal.getIncidentals().add(item.getIncidentals()));
//            }
//            // 总金额
//            if (item.getMoneySum() != null && item.getMoneySum().compareTo(new BigDecimal(0)) == 1) {
//                subtotal.setMoneySum(subtotal.getMoneySum().add(item.getMoneySum()));
//            }
//        }
        // 计算合计
        total.setNetWeight(total.getNetWeight().add(subtotal.getNetWeight()));
        total.setWeighbridgeWeight(total.getWeighbridgeWeight().add(subtotal.getWeighbridgeWeight()));
        total.setMoney(total.getMoney().add(subtotal.getMoney()));
        total.setWithhold(total.getWithhold().add(subtotal.getWithhold()));
        total.setSumWithhold(total.getSumWithhold().add(subtotal.getSumWithhold()));
        total.setIncidentals(total.getIncidentals().add(subtotal.getIncidentals()));
        total.setMoneySum(total.getMoneySum().add(subtotal.getMoneySum()));
        // 总计
        results.add(total);
        resultPage.setData((ArrayList<FinanceHgylCgResult>) results);

        return resultPage;
    }

    @Override
    public List<FinanceHgylCgResult> getChemicalRawMaterialPurchaseReport1(FinancialSalesRepostParam param) {
        // 原始数据
        List<FinanceHgylCgBo> list = financialRepostMapper.getChemicalRawMaterialPurchaseReport(param);
        // 最终返回数据
        List<FinanceHgylCgResult> results = new ArrayList<>();
        // 小计
        FinanceHgylCgResult subtotal = new FinanceHgylCgResult();
        subtotal.setPlateNumber("小计");
        subtotal.setNetWeight(new BigDecimal(0));
        subtotal.setWeighbridgeWeight(new BigDecimal(0));
        subtotal.setMoney(new BigDecimal(0));
        subtotal.setWithhold(new BigDecimal(0));
        subtotal.setSumWithhold(new BigDecimal(0));
        subtotal.setIncidentals(new BigDecimal(0));
        subtotal.setMoneySum(new BigDecimal(0));
        // 合计
        FinanceHgylCgResult total = new FinanceHgylCgResult();
        total.setPlateNumber("合计");
        total.setNetWeight(new BigDecimal(0));
        total.setWeighbridgeWeight(new BigDecimal(0));
        total.setMoney(new BigDecimal(0));
        total.setWithhold(new BigDecimal(0));
        total.setSumWithhold(new BigDecimal(0));
        total.setIncidentals(new BigDecimal(0));
        total.setMoneySum(new BigDecimal(0));
        // 合同号
        String settlementPrice = list.size() > 0 ? list.get(0).getSettlementPrice().toString() : "";
        // 计算数值
        for (FinanceHgylCgBo bo : list) {
            FinanceHgylCgResult item = CglibUtil.copy(bo, FinanceHgylCgResult.class);
            // 计算数据其他字段
            // 水分
//            BigDecimal bigDecimal1 = getWater(bo.getResult());
//            if (bigDecimal1 == null) continue;
            // 水分扣重 实物-（实物*(1000000-实际水分)/990000）
//            if (bigDecimal1.compareTo(new BigDecimal(1000)) == 1) {
//                // 1000000-实际水分
//                BigDecimal left = new BigDecimal(1000000).subtract(bigDecimal1);
//                // 扣重 实物-(实物*left/990000)
//                item.setBearing(bo.getWeighbridgeWeight().subtract(bo.getWeighbridgeWeight().multiply(left).divide(new BigDecimal(990000), 3, BigDecimal.ROUND_DOWN)));
//                // 运费扣款
//                item.setFreightWithhold(item.getBearing().multiply(item.getSettlementPrice()));
//            }
            // 磅差吨数
            item.setTunnage(bo.getPoundDifference());
//            item.setTunnage(bo.getNetWeight().subtract(bo.getWeighbridgeWeight()));
            // 磅差扣款
            BigDecimal bigDecimal = bo.getPoundDifference();
            // 如果有水分扣重，磅差需再减扣重
//            if (item.getBearing() != null && item.getBearing().compareTo(new BigDecimal(0)) == 1) {
//                bigDecimal.subtract(item.getBearing());
//            }
            // 计算磅差扣款
            if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
                item.setWithhold(bigDecimal.multiply(item.getSettlementPrice()));
            }
            // 其他扣款(其他扣款*实收吨数)
            item.setOtherWithhold(item.getOtherWithhold().multiply(item.getWeighbridgeWeight()));
            // 合计扣款
            BigDecimal bigDecimal2 = new BigDecimal(0);
            // 累加磅差扣款
            if (item.getWithhold() != null && item.getWithhold().compareTo(new BigDecimal(0)) == 1) {
                bigDecimal2 = bigDecimal2.add(item.getWithhold());
            }
            // 累加水分扣款
//            if (item.getWaterWithhold() != null && item.getWaterWithhold().compareTo(new BigDecimal(0)) == 1) {
//                bigDecimal2 = bigDecimal2.add(item.getWaterWithhold());
//            }
            // 累加运费扣款
            if (item.getFreightWithhold() != null && item.getFreightWithhold().compareTo(new BigDecimal(0)) == 1) {
                bigDecimal2 = bigDecimal2.add(item.getFreightWithhold());
            }
            // 累加其他扣款
            if (item.getOtherWithhold() != null && item.getOtherWithhold().compareTo(new BigDecimal(0)) == 1) {
                bigDecimal2 = bigDecimal2.add(item.getOtherWithhold());
            }
            item.setSumWithhold(bigDecimal2);
            // 运杂费
            BigDecimal bigDecimal3 = item.getWeighbridgeWeight().multiply(item.getFreight());
            if (bigDecimal2.compareTo(new BigDecimal(0)) == 1) {
                bigDecimal3 = bigDecimal3.subtract(bigDecimal2);
            }
            item.setIncidentals(bigDecimal3);
            // 金额
            item.setMoney(item.getWeighbridgeWeight().multiply(item.getSettlementPrice()));
            // 总金额
            item.setMoneySum(item.getMoney().add(item.getIncidentals()));
            // 将处理后的数据填入返回集合
            results.add(item);
            // 统计合计
            if (!settlementPrice.equals(item.getSettlementPrice().toString())) {
                // 添加小计
                results.add(CglibUtil.copy(subtotal, FinanceHgylCgResult.class));
                // 计算合计
                total.setNetWeight(total.getNetWeight().add(subtotal.getNetWeight()));
                total.setWeighbridgeWeight(total.getWeighbridgeWeight().add(subtotal.getWeighbridgeWeight()));
                total.setMoney(total.getMoney().add(subtotal.getMoney()));
                total.setWithhold(total.getWithhold().add(subtotal.getWithhold()));
                total.setSumWithhold(total.getSumWithhold().add(subtotal.getSumWithhold()));
                total.setIncidentals(total.getIncidentals().add(subtotal.getIncidentals()));
                total.setMoneySum(total.getMoneySum().add(subtotal.getMoneySum()));
                // 更改判断合同号
                settlementPrice = item.getSettlementPrice().toString();
                // 清空小计
                subtotal.setPlateNumber("小计");
                subtotal.setNetWeight(new BigDecimal(0));
                subtotal.setWeighbridgeWeight(new BigDecimal(0));
                subtotal.setMoney(new BigDecimal(0));
                subtotal.setWithhold(new BigDecimal(0));
                subtotal.setSumWithhold(new BigDecimal(0));
                subtotal.setIncidentals(new BigDecimal(0));
                subtotal.setMoneySum(new BigDecimal(0));
            }
            // 统计小计
            // 原发数量
            if (item.getNetWeight() != null && item.getNetWeight().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setNetWeight(subtotal.getNetWeight().add(item.getNetWeight()));
            }
            // 实收数量
            if (item.getWeighbridgeWeight() != null && item.getWeighbridgeWeight().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setWeighbridgeWeight(subtotal.getWeighbridgeWeight().add(item.getWeighbridgeWeight()));
            }
            // 金额
            if (item.getMoney() != null && item.getMoney().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setMoney(subtotal.getMoney().add(item.getMoney()));
            }
            // 扣款
            if (item.getWithhold() != null && item.getWithhold().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setWithhold(subtotal.getWithhold().add(item.getWithhold()));
            }
            // 合计扣款
            if (item.getSumWithhold() != null && item.getSumWithhold().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setSumWithhold(subtotal.getSumWithhold().add(item.getSumWithhold()));
            }
            // 运杂费
            if (item.getIncidentals() != null && item.getIncidentals().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setIncidentals(subtotal.getIncidentals().add(item.getIncidentals()));
            }
            // 总金额
            if (item.getMoneySum() != null && item.getMoneySum().compareTo(new BigDecimal(0)) == 1) {
                subtotal.setMoneySum(subtotal.getMoneySum().add(item.getMoneySum()));
            }
        }
        // 添加合计
        results.add(total);
        return results;
    }

    public static String getLastDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay1 = 0;
        //2月的平年瑞年天数
        if (month == 2) {
            // 这个api在计算2020年2月的过程中有问题
            lastDay1 = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
        } else {
            lastDay1 = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay1);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay = sdf.format(calendar.getTime()) + " 23:59:59";
        return lastDay;
    }

    public static void main(String[] args) {
        System.out.println(getLastDayOfMonth(2020, 9));
        System.out.println(getFirstDayOfMonth(2020, 9));
    }

    /**
     * 获取当前月第一天
     *
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay1 = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay1);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDay = sdf.format(calendar.getTime()) + " 00:00:00";
        return firstDay;
    }

    //    @Scheduled(fixedRate = 2000)
    @Scheduled(cron = "59 59 23 31 12 ?")
    public void setMonthBalance() {
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(ymd.format(new Date()));
        SimpleDateFormat mm = new SimpleDateFormat("MM");
        SimpleDateFormat ymds = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        System.out.println(ymd.format(new Date()));
        String moth = mm.format(new Date());
        BalanceRepostParam param = new BalanceRepostParam();
        // 获取入账的开始时间和结束时间
        param.setStartCreditTimeStr(getFirstDayOfMonth(Integer.parseInt(yyyy.format(new Date())), Integer.parseInt(mm.format(new Date()))));
        param.setEndCreditTimeStr(getLastDayOfMonth(Integer.parseInt(yyyy.format(new Date())), Integer.parseInt(mm.format(new Date()))));
        switch (moth) {
            case "12":
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(new Date());
                calendar1.set(Calendar.MONTH, calendar1.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth = format.format(calendar1.getTime());
                param.setLastSettlementMonth(lastSettlementMonth);
                param.setStartTimeStr(lastSettlementMonth+ "-26 00:00:00");
                param.setEndTimeStr(format.format(new Date()) + "-31 23:59:59");
                break;
            default:

        }
        List<BalanceRepostResult> balanceRepostResultList = financialRepostMapper.getMonthBalance1(param);
        for (BalanceRepostResult balanceRepostResult : balanceRepostResultList) {
            balanceRepostResult.setCreatedAt(new Date());
            balanceRepostResult.setSettlementMonth(format.format(new Date()));
        }
        int count = financialRepostMapper.setMonthBalance(balanceRepostResultList);
        logger.info(count + "");
    }

    @Scheduled(cron = "59 59 23 25 1,2,3,4,5,6,7,8,9,10,11 ?")
    public void setMonthBalance1() {
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat mm = new SimpleDateFormat("MM");
        SimpleDateFormat ymds = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        System.out.println(ymd.format(new Date()));
        String moth = mm.format(new Date());
        BalanceRepostParam param = new BalanceRepostParam();
        // 获取入账的开始时间和结束时间
        param.setStartCreditTimeStr(getFirstDayOfMonth(Integer.parseInt(yyyy.format(new Date())), Integer.parseInt(mm.format(new Date()))));
        param.setEndCreditTimeStr(getLastDayOfMonth(Integer.parseInt(yyyy.format(new Date())), Integer.parseInt(mm.format(new Date()))));

        switch (moth) {
            case "1":
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(new Date());
                calendar1.set(Calendar.MONTH, calendar1.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth = format.format(calendar1.getTime());
                param.setLastSettlementMonth(lastSettlementMonth);
                param.setStartTimeStr(format.format(new Date()) + "-01 00:00:00");
                param.setEndTimeStr(format.format(new Date()) + "-25 23:59:59");
                break;
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
            case "10":
            case "11":
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth1 = format.format(calendar.getTime());
                param.setLastSettlementMonth(lastSettlementMonth1);
                param.setStartTimeStr(lastSettlementMonth1 + "-26 00:00:00");
                param.setEndTimeStr(format.format(new Date()) + "-25 23:59:59");
                break;
            default:

        }
        List<BalanceRepostResult> balanceRepostResultList = financialRepostMapper.getMonthBalance1(param);
        for (BalanceRepostResult balanceRepostResult : balanceRepostResultList) {
            balanceRepostResult.setCreatedAt(new Date());
            balanceRepostResult.setSettlementMonth(format.format(new Date()));
        }
        int count = financialRepostMapper.setMonthBalance(balanceRepostResultList);
        logger.info(count + "");
    }
    public void setMonthBalance2() {
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat mm = new SimpleDateFormat("MM");
        SimpleDateFormat ymds = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String time = "2022-01-25 23:59:59";
        Date newTime =null;
        try {
            newTime = ymd.parse(time);
            System.out.println("转换以后的时间：："+newTime);//Sun Feb 02 02:02:02 CST 2020
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(ymd.format(newTime));
        String moth = mm.format(newTime);
        BalanceRepostParam param = new BalanceRepostParam();
        // 获取入账的开始时间和结束时间
        param.setStartCreditTimeStr(getFirstDayOfMonth(Integer.parseInt(yyyy.format(newTime)), Integer.parseInt(mm.format(new Date()))));
        param.setEndCreditTimeStr(getLastDayOfMonth(Integer.parseInt(yyyy.format(newTime)), Integer.parseInt(mm.format(new Date()))));

        switch (moth) {
            case "1":
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(newTime);
                calendar1.set(Calendar.MONTH, calendar1.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth = format.format(calendar1.getTime());
                param.setLastSettlementMonth(lastSettlementMonth);
                param.setStartTimeStr(format.format(newTime) + "-01 00:00:00");
                param.setEndTimeStr(format.format(newTime) + "-25 23:59:59");
                break;
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
            case "10":
            case "11":
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(newTime);
                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth1 = format.format(calendar.getTime());
                param.setLastSettlementMonth(lastSettlementMonth1);
                param.setStartTimeStr(lastSettlementMonth1 + "-26 00:00:00");
                param.setEndTimeStr(format.format(newTime) + "-25 23:59:59");
                break;
            default:

        }
        List<BalanceRepostResult> balanceRepostResultList = financialRepostMapper.getMonthBalance1(param);
        for (BalanceRepostResult balanceRepostResult : balanceRepostResultList) {
            balanceRepostResult.setCreatedAt(newTime);
            balanceRepostResult.setSettlementMonth(format.format(newTime));
        }
        int count = financialRepostMapper.setMonthBalance(balanceRepostResultList);
        logger.info(count + "");
    }

    @Override
    public List<BalanceRepostResult> getMonthBalance(BalanceRepostParam param) {
//        setMonthBalance2();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat mm = new SimpleDateFormat("MM");
        SimpleDateFormat dd = new SimpleDateFormat("dd");

        // 获取当月结算货款开始日期和结束日期
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat md = new SimpleDateFormat("MM-dd");
        String danyue = md.format(param.getStartTime()) + " " + md.format(param.getEndTime());

        param.setStartTimeStr(ymd.format(param.getStartTime()) + " 00:00:00");
        param.setEndTimeStr(ymd.format(param.getEndTime()) + " 23:59:59");
        param.setRemittanceStartTimeStr(ymd.format(param.getRemittanceStartTime()) + " 00:00:00");
        param.setRemittanceEndTimeStr(ymd.format(param.getRemittanceEndTime()) + " 23:59:59");
        if ("01-01 01-25".equals(danyue) ||
                "01-26 02-25".equals(danyue) ||
                "02-26 03-25".equals(danyue) ||
                "03-26 04-25".equals(danyue) ||
                "04-26 05-25".equals(danyue) ||
                "05-26 06-25".equals(danyue) ||
                "06-26 07-25".equals(danyue) ||
                "07-26 08-25".equals(danyue) ||
                "08-26 09-25".equals(danyue) ||
                "09-26 10-25".equals(danyue) ||
                "10-26 11-25".equals(danyue) ||
                "11-26 12-31".equals(danyue)
        ) {
            // 获取汇款的月份
            param.setSettlementMonth(format.format(param.getEndTime()));
            // 获取入账的开始时间和结束时间
            param.setStartCreditTimeStr(getFirstDayOfMonth(Integer.parseInt(yyyy.format(param.getEndTime())), Integer.parseInt(mm.format(param.getEndTime()))));
            param.setEndCreditTimeStr(getLastDayOfMonth(Integer.parseInt(yyyy.format(param.getEndTime())), Integer.parseInt(mm.format(param.getEndTime()))));
            if ("01-01 01-25".equals(danyue)) {
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(param.getStartTime());
                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth = format.format(calendar.getTime());
                param.setLastSettlementMonth(lastSettlementMonth);
            } else {
                param.setLastSettlementMonth(format.format(param.getStartTime()));
            }
        } else {
            // 获取入账的开始时间和结束时间
            param.setStartCreditTimeStr(ymd.format(param.getStartTime()) + " 00:00:00");
            param.setEndCreditTimeStr(ymd.format(param.getEndTime()) + " 23:59:59");
            // 如果时间段在单月的26号-月底之间
            if (Integer.parseInt(dd.format(param.getStartTime())) >= 26
                    && Integer.parseInt(dd.format(param.getEndTime())) >= 26
                    && format.format(param.getStartTime()).equals(format.format(param.getEndTime()))
            ) {
                param.setStartCreditTimeStr("0000-00-00 00:00:00");
                param.setEndCreditTimeStr("0000-00-00 00:00:00");
            }
            if (Integer.parseInt(dd.format(param.getStartTime())) >= 26
                    && Integer.parseInt(dd.format(param.getEndTime())) >= 26
                    && format.format(param.getStartTime()).equals(format.format(param.getEndTime()))
            ) {
                param.setStartCreditTimeStr(ymd.format(param.getStartTime()) + " 00:00:00");
                param.setEndCreditTimeStr(ymd.format(param.getEndTime()) + " 23:59:59");
            }
            String text1 = format.format(param.getStartTime()) + "-01";
            Temporal temporal1 = LocalDate.parse(text1);
            String text2 = format.format(param.getEndTime()) + "-01";
            Temporal temporal2 = LocalDate.parse(text2);
            System.out.println(ChronoUnit.MONTHS.between(temporal1, temporal2));
            System.out.println(format.format(param.getEndTime()).equals(format.format(new Date())));
            System.out.println(ChronoUnit.MONTHS.between(temporal1, temporal2) == 1);
            if (Integer.parseInt(dd.format(param.getStartTime())) >= 26
                    && Integer.parseInt(dd.format(param.getEndTime())) <= 31
                    && ChronoUnit.MONTHS.between(temporal1, temporal2) == 1
                    && format.format(param.getEndTime()).equals(format.format(new Date()))
            ) {
                param.setSettlementMonth(format.format(param.getEndTime()));
                param.setStartCreditTimeStr(getFirstDayOfMonth(Integer.parseInt(yyyy.format(param.getEndTime())), Integer.parseInt(mm.format(param.getEndTime()))));
                param.setEndCreditTimeStr(getLastDayOfMonth(Integer.parseInt(yyyy.format(param.getEndTime())), Integer.parseInt(mm.format(param.getEndTime()))));
            }
            if ("01".equals(mm.format(param.getStartTime())) || "12".equals(mm.format(param.getStartTime()))) {
                // 获取上一个月 便于获取期初余额 期初余额=开始时间的上一个月月底额度
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(param.getStartTime());
                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
                String lastSettlementMonth = format.format(calendar.getTime());
                param.setLastSettlementMonth(lastSettlementMonth);
            } else {
                param.setLastSettlementMonth(format.format(param.getStartTime()));
            }
        }

        if (dateCompare(format.format(param.getStartTime())) != 1) {
            List<BalanceRepostResult> balanceRepostResultList = null;
            if (param.getCategoryId() == null && param.getMaterialId() == null) {
                balanceRepostResultList = financialRepostMapper.getMonthBalance1(param);
            } else {
                balanceRepostResultList = financialRepostMapper.getMonthBalance(param);
            }
            return balanceRepostResultList;
        }
        return null;

    }

    @Override
    public Map<String,Object> savaBalance(BalanceRepostParam param){
        Map<String,Object> resultMap = new HashMap<>();
        List<BalanceRepostResult> list = getMonthBalance(param);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(new Date());
        try {
            //Date date = DateUtil.parse(now);
            Date date = sdf.parse(now);
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map =new HashMap<>();
                map.put("companyId",list.get(i).getCompanyId());
                map.put("settlementMonth",param.getSaveMonth());
                //20231129  BiMing  保存异常，发现数据库中settlement_month表有两条一模一样的数据
                List<BalanceRepostResult> balist=financialRepostMapper.getBalanceByMonthList(map);
                BalanceRepostResult balance=new BalanceRepostResult();
                if (!balist.isEmpty()){
                    balance=balist.get(0);
                }
               // BalanceRepostResult balance = financialRepostMapper.getBalanceByMonth(list.get(i).getCompanyId(),param.getSaveMonth());
                list.get(i).setSettlementMonth(param.getSaveMonth());
                list.get(i).setUpdatedAt(now);
                if(null == balance){
                    financialRepostMapper.savaBalance(list.get(i));
                }else {
                    list.get(i).setCompanyName(now);
                    financialRepostMapper.updateBalance(list.get(i));
                }
            }
            resultMap.put("code","200");
            resultMap.put("msg","保存完成");
        }catch (Exception e){
            resultMap.put("code","201");
            resultMap.put("msg","保存异常");
            logger.info(e.getMessage());
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 销售报表
     *
     * @param param
     * @return
     */
    @Override
    public List<SalesReport> getSalesRepost(FinancialSalesRepostParam param) {
        List<SalesReport> repostResults = new ArrayList<>();
        List<SalesReport> repostResultList = financialRepostMapper.getSalesRepost(param);
        // 以物料做分组
        Map<String, List<SalesReport>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(SalesReport::getCompanyName, Collectors.toList()));
        SalesReport financialRepostHJ = new SalesReport();
        financialRepostHJ.setCompanyName("合计");
        for (String str : collect1.keySet()) {
            // 再以物料做分组下再以公司做分组
            Map<String, List<SalesReport>> collect2 = collect1.get(str).stream()
                    .collect(Collectors.groupingBy(item -> item.getSettlementPrice() + "_" + item.getMaterialName()));
            SalesReport financialRepostXJ = new SalesReport();
            financialRepostXJ.setMaterialName("小计");
            for (String str1 : collect2.keySet()) {
                // 公司累计
                SalesReport financialRepostGS = new SalesReport();
                for (SalesReport financialRepostResult : collect2.get(str1)) {
                    // 赋值物料名称
                    financialRepostGS.setMaterialName(financialRepostResult.getMaterialName());
                    // 赋值公司名称
                    financialRepostGS.setCompanyName(financialRepostResult.getCompanyName());
                    // 累计公司的货重
                    financialRepostGS.setWeighbridgeWeight(financialRepostGS.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));


                    // 赋值公司含税单价
                    financialRepostGS.setSettlementPrice(financialRepostResult.getSettlementPrice());
                    // 累计公司含税金额
                    financialRepostGS.setSettlementPriceSum(financialRepostGS.getSettlementPriceSum().add(financialRepostResult.getSettlementPriceSum()));
                    // 累计公司含税垫付运费
                    financialRepostGS.setFreightSum(financialRepostGS.getFreightSum().add(financialRepostResult.getFreightSum()));
                    // 累计公司含税装车费
                    financialRepostGS.setInstallSum(financialRepostGS.getInstallSum().add(financialRepostResult.getInstallSum()));
                    // 累计公司含税包装费
                    financialRepostGS.setPackageSum(financialRepostGS.getPackageSum().add(financialRepostResult.getPackageSum()));
                    // 累计公司含税其他费用
                    financialRepostGS.setOtherSum(financialRepostGS.getOtherSum().add(financialRepostResult.getOtherSum()));
                    // 累计公司含税专线费
                    financialRepostGS.setSpecialSum(financialRepostGS.getSpecialSum().add(financialRepostResult.getSpecialSum()));
                    // 累计公司含税合计费用
                    financialRepostGS.setMoneySum(financialRepostGS.getMoneySum().add(financialRepostResult.getMoneySum()));


                    // 累计公司含税金额
                    financialRepostGS.setTaxRate(financialRepostResult.getTaxRate());
                    // 累计公司含税垫付运费
                    financialRepostGS.setFreightTaxRate(financialRepostResult.getFreightTaxRate());
                    // 累计公司含税装车费
                    financialRepostGS.setInstallTaxRate(financialRepostResult.getInstallTaxRate());
                    // 累计公司含税包装费
                    financialRepostGS.setPackageTaxRate(financialRepostResult.getPackageTaxRate());
                    // 累计公司含税其他费用
                    financialRepostGS.setOtherTaxRate(financialRepostResult.getOtherTaxRate());
                    // 累计公司含税专线费
                    financialRepostGS.setSpecialTaxRate(financialRepostResult.getSpecialTaxRate());

                    // 赋值公司不含税单价
                    financialRepostGS.setSettlementPriceNo(financialRepostResult.getSettlementPriceNo());
                    // 累计公司不含税金额
                    financialRepostGS.setSettlementPriceSumNo(financialRepostGS.getSettlementPriceSumNo().add(financialRepostResult.getSettlementPriceSumNo()));
                    // 累计公司不含税垫付运费
                    financialRepostGS.setFreightSumNo(financialRepostGS.getFreightSumNo().add(financialRepostResult.getFreightSumNo()));
                    // 累计公司不含税装车费
                    financialRepostGS.setInstallSumNo(financialRepostGS.getInstallSumNo().add(financialRepostResult.getInstallSumNo()));
                    // 累计公司不含税包装费
                    financialRepostGS.setPackageSumNo(financialRepostGS.getPackageSumNo().add(financialRepostResult.getPackageSumNo()));
                    // 累计公司不含税其他费用
                    financialRepostGS.setOtherSumNo(financialRepostGS.getOtherSumNo().add(financialRepostResult.getOtherSumNo()));
                    // 累计公司不含税专线费
                    financialRepostGS.setSpecialSumNo(financialRepostGS.getSpecialSumNo().add(financialRepostResult.getSpecialSumNo()));
                    // 累计公司不含税合计费用
                    financialRepostGS.setMoneySumNo(financialRepostGS.getMoneySumNo().add(financialRepostResult.getMoneySumNo()));

                }
                repostResults.add(financialRepostGS);
                // 赋值公司名称
                financialRepostXJ.setCompanyName(financialRepostGS.getCompanyName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostGS.getWeighbridgeWeight()));
                // 累计公司含税金额
                financialRepostXJ.setSettlementPriceSum(financialRepostXJ.getSettlementPriceSum().add(financialRepostGS.getSettlementPriceSum()));
                // 累计公司含税垫付运费
                financialRepostXJ.setFreightSum(financialRepostXJ.getFreightSum().add(financialRepostGS.getFreightSum()));
                // 累计公司含税装车费
                financialRepostXJ.setInstallSum(financialRepostXJ.getInstallSum().add(financialRepostGS.getInstallSum()));
                // 累计公司含税包装费
                financialRepostXJ.setPackageSum(financialRepostXJ.getPackageSum().add(financialRepostGS.getPackageSum()));
                // 累计公司含税其他费用
                financialRepostXJ.setOtherSum(financialRepostXJ.getOtherSum().add(financialRepostGS.getOtherSum()));
                // 累计公司含税专线费
                financialRepostXJ.setSpecialSum(financialRepostXJ.getSpecialSum().add(financialRepostGS.getSpecialSum()));
                // 累计公司含税合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostGS.getMoneySum()));


                // 累计公司不含税金额
                financialRepostXJ.setSettlementPriceSumNo(financialRepostXJ.getSettlementPriceSumNo().add(financialRepostGS.getSettlementPriceSumNo()));
                // 累计公司不含税垫付运费
                financialRepostXJ.setFreightSumNo(financialRepostXJ.getFreightSumNo().add(financialRepostGS.getFreightSumNo()));
                // 累计公司不含税装车费
                financialRepostXJ.setInstallSumNo(financialRepostXJ.getInstallSumNo().add(financialRepostGS.getInstallSumNo()));
                // 累计公司不含税包装费
                financialRepostXJ.setPackageSumNo(financialRepostXJ.getPackageSumNo().add(financialRepostGS.getPackageSumNo()));
                // 累计公司不含税其他费用
                financialRepostXJ.setOtherSumNo(financialRepostXJ.getOtherSumNo().add(financialRepostGS.getOtherSumNo()));
                // 累计公司不含税专线费
                financialRepostXJ.setSpecialSumNo(financialRepostXJ.getSpecialSumNo().add(financialRepostGS.getSpecialSumNo()));
                // 累计公司不含税合计费用
                financialRepostXJ.setMoneySumNo(financialRepostXJ.getMoneySumNo().add(financialRepostGS.getMoneySumNo()));
            }
            // 赋值公司含税单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getSettlementPriceSum().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            // 赋值公司不含税单价
            financialRepostXJ.setSettlementPriceNo(financialRepostXJ.getSettlementPriceSumNo().divide(financialRepostXJ.getWeighbridgeWeight(), 4, BigDecimal.ROUND_HALF_UP));

            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));

            // 累计公司含税金额
            financialRepostHJ.setSettlementPriceSum(financialRepostHJ.getSettlementPriceSum().add(financialRepostXJ.getSettlementPriceSum()));
            // 累计公司含税垫付运费
            financialRepostHJ.setFreightSum(financialRepostHJ.getFreightSum().add(financialRepostXJ.getFreightSum()));
            // 累计公司含税装车费
            financialRepostHJ.setInstallSum(financialRepostHJ.getInstallSum().add(financialRepostXJ.getInstallSum()));
            // 累计公司含税包装费
            financialRepostHJ.setPackageSum(financialRepostHJ.getPackageSum().add(financialRepostXJ.getPackageSum()));
            // 累计公司含税其他费用
            financialRepostHJ.setOtherSum(financialRepostHJ.getOtherSum().add(financialRepostXJ.getOtherSum()));
            // 累计公司含税专线费
            financialRepostHJ.setSpecialSum(financialRepostHJ.getSpecialSum().add(financialRepostXJ.getSpecialSum()));
            // 累计公司含税合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()));


            // 累计公司不含税金额
            financialRepostHJ.setSettlementPriceSumNo(financialRepostHJ.getSettlementPriceSumNo().add(financialRepostXJ.getSettlementPriceSumNo()));
            // 累计公司不含税垫付运费
            financialRepostHJ.setFreightSumNo(financialRepostHJ.getFreightSumNo().add(financialRepostXJ.getFreightSumNo()));
            // 累计公司不含税装车费
            financialRepostHJ.setInstallSumNo(financialRepostHJ.getInstallSumNo().add(financialRepostXJ.getInstallSumNo()));
            // 累计公司不含税包装费
            financialRepostHJ.setPackageSumNo(financialRepostHJ.getPackageSumNo().add(financialRepostXJ.getPackageSumNo()));
            // 累计公司不含税其他费用
            financialRepostHJ.setOtherSumNo(financialRepostHJ.getOtherSumNo().add(financialRepostXJ.getOtherSumNo()));
            // 累计公司不含税专线费
            financialRepostHJ.setSpecialSumNo(financialRepostHJ.getSpecialSumNo().add(financialRepostXJ.getSpecialSumNo()));
            // 累计公司不含税合计费用
            financialRepostHJ.setMoneySumNo(financialRepostHJ.getMoneySumNo().add(financialRepostXJ.getMoneySumNo()));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getSettlementPriceSum().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        financialRepostHJ.setSettlementPriceNo(financialRepostHJ.getSettlementPriceSumNo().divide(financialRepostHJ.getWeighbridgeWeight(), 4, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    /**
     * 销售明细报表
     *
     * @param param
     * @return
     */
    @Override
    public List<SalesReport> getSalesMxRepost(FinancialSalesRepostParam param) {
        List<SalesReport> repostResults = new ArrayList<>();
        List<SalesReport> repostResultList = financialRepostMapper.getSalesRepost(param);
        // 以物料做分组
        Map<String, List<SalesReport>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(SalesReport::getCompanyName, Collectors.toList()));
        SalesReport financialRepostHJ = new SalesReport();
        financialRepostHJ.setCompanyName("合计");
        for (String str : collect1.keySet()) {
            SalesReport financialRepostXJ = new SalesReport();
            financialRepostXJ.setMaterialName("小计");
            for (SalesReport financialRepostResult : collect1.get(str)) {
                repostResults.add(financialRepostResult);
                // 赋值公司名称
                financialRepostXJ.setCompanyName(financialRepostResult.getCompanyName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));


                // 累计公司含税金额
                financialRepostXJ.setSettlementPriceSum(financialRepostXJ.getSettlementPriceSum().add(financialRepostResult.getSettlementPriceSum()));
                // 累计公司含税垫付运费
                financialRepostXJ.setFreightSum(financialRepostXJ.getFreightSum().add(financialRepostResult.getFreightSum()));
                // 累计公司含税装车费
                financialRepostXJ.setInstallSum(financialRepostXJ.getInstallSum().add(financialRepostResult.getInstallSum()));
                // 累计公司含税包装费
                financialRepostXJ.setPackageSum(financialRepostXJ.getPackageSum().add(financialRepostResult.getPackageSum()));
                // 累计公司含税其他费用
                financialRepostXJ.setOtherSum(financialRepostXJ.getOtherSum().add(financialRepostResult.getOtherSum()));
                // 累计公司含税专线费
                financialRepostXJ.setSpecialSum(financialRepostXJ.getSpecialSum().add(financialRepostResult.getSpecialSum()));
                // 累计公司含税合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostResult.getMoneySum()));


                // 累计公司含税金额
                financialRepostXJ.setTaxRate(financialRepostResult.getTaxRate());
                // 累计公司含税垫付运费
                financialRepostXJ.setFreightTaxRate(financialRepostResult.getFreightTaxRate());
                // 累计公司含税装车费
                financialRepostXJ.setInstallTaxRate(financialRepostResult.getInstallTaxRate());
                // 累计公司含税包装费
                financialRepostXJ.setPackageTaxRate(financialRepostResult.getPackageTaxRate());
                // 累计公司含税其他费用
                financialRepostXJ.setOtherTaxRate(financialRepostResult.getOtherTaxRate());
                // 累计公司含税专线费
                financialRepostXJ.setSpecialTaxRate(financialRepostResult.getSpecialTaxRate());


                // 累计公司不含税金额
                financialRepostXJ.setSettlementPriceSumNo(financialRepostXJ.getSettlementPriceSumNo().add(financialRepostResult.getSettlementPriceSumNo()));
                // 累计公司不含税垫付运费
                financialRepostXJ.setFreightSumNo(financialRepostXJ.getFreightSumNo().add(financialRepostResult.getFreightSumNo()));
                // 累计公司不含税装车费
                financialRepostXJ.setInstallSumNo(financialRepostXJ.getInstallSumNo().add(financialRepostResult.getInstallSumNo()));
                // 累计公司不含税包装费
                financialRepostXJ.setPackageSumNo(financialRepostXJ.getPackageSumNo().add(financialRepostResult.getPackageSumNo()));
                // 累计公司不含税其他费用
                financialRepostXJ.setOtherSumNo(financialRepostXJ.getOtherSumNo().add(financialRepostResult.getOtherSumNo()));
                // 累计公司不含税专线费
                financialRepostXJ.setSpecialSumNo(financialRepostXJ.getSpecialSumNo().add(financialRepostResult.getSpecialSumNo()));
                // 累计公司不含税合计费用
                financialRepostXJ.setMoneySumNo(financialRepostXJ.getMoneySumNo().add(financialRepostResult.getMoneySumNo()));
            }
            // 赋值公司含税单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getSettlementPriceSum().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            // 赋值公司不含税单价
            financialRepostXJ.setSettlementPriceNo(financialRepostXJ.getSettlementPriceSumNo().divide(financialRepostXJ.getWeighbridgeWeight(), 4, BigDecimal.ROUND_HALF_UP));
            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));
            // 赋值公司含税单价
            financialRepostHJ.setSettlementPrice(financialRepostXJ.getSettlementPrice());
            // 累计公司含税金额
            financialRepostHJ.setSettlementPriceSum(financialRepostHJ.getSettlementPriceSum().add(financialRepostXJ.getSettlementPriceSum()));
            // 累计公司含税垫付运费
            financialRepostHJ.setFreightSum(financialRepostHJ.getFreightSum().add(financialRepostXJ.getFreightSum()));
            // 累计公司含税装车费
            financialRepostHJ.setInstallSum(financialRepostHJ.getInstallSum().add(financialRepostXJ.getInstallSum()));
            // 累计公司含税包装费
            financialRepostHJ.setPackageSum(financialRepostHJ.getPackageSum().add(financialRepostXJ.getPackageSum()));
            // 累计公司含税其他费用
            financialRepostHJ.setOtherSum(financialRepostHJ.getOtherSum().add(financialRepostXJ.getOtherSum()));
            // 累计公司含税专线费
            financialRepostHJ.setSpecialSum(financialRepostHJ.getSpecialSum().add(financialRepostXJ.getSpecialSum()));
            // 累计公司含税合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()));


            // 赋值公司不含税单价
            financialRepostHJ.setSettlementPriceNo(financialRepostXJ.getSettlementPriceNo());
            // 累计公司不含税金额
            financialRepostHJ.setSettlementPriceSumNo(financialRepostHJ.getSettlementPriceSumNo().add(financialRepostXJ.getSettlementPriceSumNo()));
            // 累计公司不含税垫付运费
            financialRepostHJ.setFreightSumNo(financialRepostHJ.getFreightSumNo().add(financialRepostXJ.getFreightSumNo()));
            // 累计公司不含税装车费
            financialRepostHJ.setInstallSumNo(financialRepostHJ.getInstallSumNo().add(financialRepostXJ.getInstallSumNo()));
            // 累计公司不含税包装费
            financialRepostHJ.setPackageSumNo(financialRepostHJ.getPackageSumNo().add(financialRepostXJ.getPackageSumNo()));
            // 累计公司不含税其他费用
            financialRepostHJ.setOtherSumNo(financialRepostHJ.getOtherSumNo().add(financialRepostXJ.getOtherSumNo()));
            // 累计公司不含税专线费
            financialRepostHJ.setSpecialSumNo(financialRepostHJ.getSpecialSumNo().add(financialRepostXJ.getSpecialSumNo()));
            // 累计公司不含税合计费用
            financialRepostHJ.setMoneySumNo(financialRepostHJ.getMoneySumNo().add(financialRepostXJ.getMoneySumNo()));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getSettlementPriceSum().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        financialRepostHJ.setSettlementPriceNo(financialRepostHJ.getSettlementPriceSumNo().divide(financialRepostHJ.getWeighbridgeWeight(), 4, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    @Override
    public List<FinancialRepostResult> getStatisticalBills(FinancialSalesRepostParam param) {
        List<FinancialRepostResult> repostResults = new ArrayList<>();
        List<FinancialRepostResult> repostResultList = financialRepostMapper.getStatisticalBills(param);
        if (repostResultList.size() < 1) {
            return repostResultList;
        }
        // 以物料做分组
        Map<String, List<FinancialRepostResult>> collect1 = repostResultList.stream()
                .collect(Collectors.groupingBy(FinancialRepostResult::getMaterialName, Collectors.toList()));
        FinancialRepostResult financialRepostHJ = new FinancialRepostResult();
        financialRepostHJ.setCompanyName("合计");
        for (String str : collect1.keySet()) {
            // 再以物料做分组下再以公司做分组
            Map<String, List<FinancialRepostResult>> collect2 = collect1.get(str).stream()
                    .collect(Collectors.groupingBy(item -> item.getSettlementPrice() + "_" + item.getMaterialName()));
            FinancialRepostResult financialRepostXJ = new FinancialRepostResult();
            financialRepostXJ.setCompanyName("小计");
            for (String str1 : collect2.keySet()) {
                // 公司累计
                FinancialRepostResult financialRepostGS = new FinancialRepostResult();
                for (FinancialRepostResult financialRepostResult : collect2.get(str1)) {
//                    financialRepostResult.setMoney(financialRepostResult.getWeighbridgeWeight().multiply(financialRepostResult.getSettlementPrice()).setScale(2,BigDecimal.ROUND_HALF_UP));
                    // 赋值物料名称
                    financialRepostGS.setMaterialName(financialRepostResult.getMaterialName());
                    // 赋值公司名称
                    financialRepostGS.setCompanyName(financialRepostResult.getCompanyName());
                    // 累计公司的货重
                    financialRepostGS.setWeighbridgeWeight(financialRepostGS.getWeighbridgeWeight().add(financialRepostResult.getWeighbridgeWeight()));
                    // 赋值公司单价
                    financialRepostGS.setSettlementPrice(financialRepostResult.getSettlementPrice().setScale(2,BigDecimal.ROUND_HALF_UP));
                    // 累计公司金额

//                    financialRepostGS.setMoney(financialRepostResult.getWeighbridgeWeight().multiply(financialRepostResult.getSettlementPrice()).setScale(2,BigDecimal.ROUND_HALF_UP));
                    financialRepostGS.setMoney(financialRepostGS.getMoney().add(financialRepostResult.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                    // 累计公司垫付运费1
                    financialRepostGS.setAdvance(financialRepostGS.getAdvance().add(financialRepostResult.getAdvance()));
                    // 累计公司装车费
                    financialRepostGS.setAssembleMoney(financialRepostGS.getAssembleMoney().add(financialRepostResult.getAssembleMoney()));
                    // 累计公司包装费
                    financialRepostGS.setPack(financialRepostGS.getPack().add(financialRepostResult.getPack()));
                    // 累计公司其他费用
                    financialRepostGS.setOther(financialRepostGS.getOther().add(financialRepostResult.getOther()));
                    // 累计公司合计费用
                    financialRepostGS.setMoneySum(financialRepostGS.getMoneySum().add(financialRepostResult.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));

                    repostResults.add(financialRepostResult);
                }
                // 赋值公司名称
//                financialRepostXJ.setCompanyName(financialRepostGS.getCompanyName());
                // 累计公司的货重
                financialRepostXJ.setWeighbridgeWeight(financialRepostXJ.getWeighbridgeWeight().add(financialRepostGS.getWeighbridgeWeight()));
                // 累计公司金额
                financialRepostXJ.setMoney(financialRepostXJ.getMoney().add(financialRepostGS.getMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                // 累计公司垫付运费
                financialRepostXJ.setAdvance(financialRepostXJ.getAdvance().add(financialRepostGS.getAdvance()));
                // 累计公司装车费
                financialRepostXJ.setAssembleMoney(financialRepostXJ.getAssembleMoney().add(financialRepostGS.getAssembleMoney()).setScale(2,BigDecimal.ROUND_HALF_UP));
                // 累计公司包装费
                financialRepostXJ.setPack(financialRepostXJ.getPack().add(financialRepostGS.getPack()));
                // 累计公司其他费用
                financialRepostXJ.setOther(financialRepostXJ.getOther().add(financialRepostGS.getOther()));
                // 累计公司合计费用
                financialRepostXJ.setMoneySum(financialRepostXJ.getMoneySum().add(financialRepostGS.getMoneySum()).setScale(2,BigDecimal.ROUND_HALF_UP));
            }
            // 赋值公司单价
            financialRepostXJ.setSettlementPrice(financialRepostXJ.getMoney().divide(financialRepostXJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
            repostResults.add(financialRepostXJ);
            // 累计公司的货重
            financialRepostHJ.setWeighbridgeWeight(financialRepostHJ.getWeighbridgeWeight().add(financialRepostXJ.getWeighbridgeWeight()));
            // 累计公司金额
            financialRepostHJ.setMoney(financialRepostHJ.getMoney().add(financialRepostXJ.getMoney()));
            // 累计公司垫付运费
            financialRepostHJ.setAdvance(financialRepostHJ.getAdvance().add(financialRepostXJ.getAdvance()));
            // 累计公司装车费
            financialRepostHJ.setAssembleMoney(financialRepostHJ.getAssembleMoney().add(financialRepostXJ.getAssembleMoney()));
            // 累计公司包装费
            financialRepostHJ.setPack(financialRepostHJ.getPack().add(financialRepostXJ.getPack()));
            // 累计公司其他费用
            financialRepostHJ.setOther(financialRepostHJ.getOther().add(financialRepostXJ.getOther()));
            // 累计公司合计费用
            financialRepostHJ.setMoneySum(financialRepostHJ.getMoneySum().add(financialRepostXJ.getMoneySum()));
        }
        // 赋值公司单价
        financialRepostHJ.setSettlementPrice(financialRepostHJ.getMoney().divide(financialRepostHJ.getWeighbridgeWeight(), 2, BigDecimal.ROUND_HALF_UP));
        repostResults.add(financialRepostHJ);

        return repostResults;
    }

    /**
     * @return
     * @title: dateCompare
     * @description: 比较日期大小
     */
    public static int dateCompare(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        String dateFirst = dateFormat.format(new Date());
        int dateFirstIntVal = Integer.parseInt(date.replace("-", ""));
        int dateLastIntVal = Integer.parseInt(dateFirst);
        // 开始时间大于当前时间
        if (dateFirstIntVal > dateLastIntVal) {
            return 1;
        } else
            //开始时间小于当前时间
            if (dateFirstIntVal < dateLastIntVal) {
                return -1;
            }
        //开始时间等于当前时间
        return 0;
    }

    /**
     * 取水分质检值
     */
    public BigDecimal getWater(String result) {
        if (result == null) return null;
        try {
            JSONArray jsonArray = JSONArray.parseArray(result);
            for (Object obj : jsonArray) {
                JSONObject json = JSON.parseObject(obj.toString());
                String name = json.getString("chinese");
                if ("水分".equals(name) || "全水分".equals(name)) {
                    BigDecimal value = json.getBigDecimal("value");
                    return value;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        return null;
    }
}
